var BCTimer = function() {
	var _this = this;
	var code = 99999;
	var run = function(code, millisec) {
		var code = setInterval(code, millisec);
	};
	var stop = function() {
		window.clearInterval(_this.code);
	};
}
var GameView = {
	copy: {
		btn: $("#copyAddrBtn"),
		click: function() {
			GameView.copy.copyValue($("#gameAddr").html(), function() {
				AlertView.layer.msg('游戏ID已经复制');
			});
		},
		copyValue: function(msg, success) {
			input = document.createElement('input');
			document.body.appendChild(input);
			input.setAttribute('value', msg);
			input.select();
			if(document.execCommand('copy')) {
				document.execCommand('copy');
				success();
			}
			document.body.removeChild(input);
		},
	},
	query: function(query) {
		if(typeof BC.bcInstance != "undefined") {
			query();
		} else {
			BC.onFinish.push(query);
		}
	},
	code: 0,

	actionLog: function(log) {}
};
GameView.ws = {
	stateData: {},
	timeout: 0,
	error: function(data, p) {
		if(typeof data.result != "undefined" && data.result != "1") {
			BC_Notify.show({
				msg: data.msg,
				mask: false,
			})
			return true;
		};
		return false;
	},
	state: function(data, p) {
		if(GameView.ws.error(data, p)) return;
		BC_Notify.show({
			msg: data.msg ? data.msg : "游戏已进入出拳阶段，你必须在限定时间内出拳",
			mask: false,
		});
		console.log(data);
		GameView.ws.timeout = data.timeout;
		GameView.ws.stateData = data;
		GameView.ws.search(data.gameId);
	},
	msg: function(data) {
		BC_Notify.show({
			msg: data.msg,
			mask: false,
		});
	},
	peer: function(data1) {
		if(GameView.ws.error(data1)) return;
		BC_Notify.show({
			msg: data1.msg,
			mask: false,
		});
		console.log("查询状态");
		BC_POST.search({
			searchId: data1.gameId
		}).then(function(data) {
			GameView.setGameData(data.data[0]);
		}).catch(function(err) {
			console.log(err);
		});
	},
	search: function(gameId) {
		console.log("查询状态");
		BC_POST.search({
			searchId: gameId
		}).then(function(data) {
			GameView.setGameData(data.data[0]);

		}).catch(function(err) {
			console.log(err);
		});
	},
	timer: function(timeout) {
		if(GameView.ws.timeout <= 0) {
			if(GameView.ws.stateData.state == 2) {
				$("#chuquan").addClass("active");
			} else if(GameView.ws.stateData.state == 3) {
				$("#kaiquan").addClass("active");
			}
			return;
		};
		GameView.ws.timeout = GameView.ws.timeout - 1;
		if(GameView.ws.stateData.state == 2) {
			$(".chuquantr .bc_center .timeout x").html(GameView.ws.timeout);
		} else if(GameView.ws.stateData.state == 3) {
			$(".kaiquantr .bc_center .timeout x").html(GameView.ws.timeout);
		}

	}
}
GameView.gameData = {};
GameView.param = {};
GameView.setGameData = function(data) {
	var tmp;
	data.changeValue = function(a, b) {
		var tmp = this[a];
		this[a] = this[b];
		this[b] = tmp;
	};
	if(App.accounts[0] == data.player2) {
		data.changeValue("player1", "Player2");
		data.changeValue("blindedBid1", "blindedBid2");
		data.changeValue("choice1", "choice2");
		data.changeValue("status1", "status1");
		if(data.winner == 1){
			data.winner = 2;
		}else if(data.winner == 2){
			data.winner = 1;
		}
	}
	data.wstimeout = GameView.ws.timeout;
	GameView.gameData = data;
	console.log(GameView.gameData);
	$("tr.infotr").remove();
	GameView.gameData.stateData = GameView.ws.stateData
	var html = template("gameTmp", GameView.gameData);
	$("table").append(html);
	$(".bc_center.eth").html(data.stake / Math.pow(10, 18) + "ETH");

}
GameView.init = function(delegate, param) {
	GameView.param = param;
	$("#gameAddr").html(param.gameId);
	$(".eth").html(param.stake / Math.pow(10, 18) + "ETH");
	GameView.copy.btn.on("click", GameView.copy.click);
	chuquan.init();
	kaiquan.init();
	jiesuan.init();

	BC_WS.addObserver("msg", function(data, p) {
		if(typeof data.code != "undefined" && data.code != "500") return;
		GameView.ws.msg(data);
	}, true);
	BC_WS.addObserver("state", function(data, p) {
		if(typeof data.code != "undefined" && data.code != "500") return;
		GameView.ws.state(data);
	}, true);
	BC_WS.addObserver("peer-status", function(data, p) {
		if(typeof data.code != "undefined" && data.code != "500") return;
		GameView.ws.peer(data);
	}, true);

	setInterval(GameView.ws.timer, 1000);

};
$(function() {
	App.init();
	App.canUse().then(function(b) {
		BC_WS.init(BCConfig.wsUrl1);
		BC_WS.onopen = function() {

			var cssCode = ".colorId_" + App.accounts[0] + "{color:yellow;}";
			APPTool.loadCssCode(cssCode);

			var f = function(data) {
				if(typeof data.msg != "undefined" && data.msg != "") {
					AlertView.layer.msg(data.msg);
				}
			};
			BC_WS.addObserver("login-resp", f);
			BC_WS.addObserver("iam-resp", f);
			BC_WS.send({
				cmd: "login",
				gameId: GameParam.gameId,
				player: App.accounts[0],
				secret: md5(GameParam.gameId + App.accounts[0])
			});

		};
		GameView.init({}, GameParam);
	}).catch(function(err) {
		console.log(err);
		if(typeof err != "undefined" && typeof err.message != "undefined") {
			err.msg = err.message;
		}
		BC_Notify.show({
			msg: err.msg,
			actions: [{
				type: 'btn',
				title: "刷新",
				func: function(index) {
					BC_Notify.close();
					history.go(0);
				}
			}]
		});
	});
});